<template>
<!-- 收藏 -->
  <van-button
    @click="onColl"
    :class="{ collected: value }"
    :icon="value ? 'star' : 'star-o'"
    :loading="loading"
  />
</template>

<script>
import { addCollect, deleteCollect } from "@/api/article";
export default {
  name: "CollectArticle",
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    articleId: {
      type: [Number, Object, String],
      required: true,
    },
  },
  methods: {
    async onColl() {
      console.log(1);
      this.loading = true;
      try {
        if (this.value) {
          // 已收藏  取消
          await deleteCollect(this.articleId);
        } else {
          // 未收藏 添加收藏
          await addCollect(this.articleId);
        }
        // 更新视图  v-model value - input
        this.$emit("input", !this.value);
        this.$toast.success(!this.value?'收藏成功':'取消')
        
      } catch (err) {
        console.log("收藏失败");
        this.$toast.fail('操作失败')
      }

      this.loading = false;
    },
  },
  data() {
    return {
      loading: false,
    };
  },
};
</script>

<style  scoped lang="less">
.collected {
  .van-icon {
    color: #ffa500;
  }
}
</style>